//
//  CustomImageViewer.m
//  Mehfile Zainab
//
//  Created by Sitara Badourali on 25/02/12.
//  Copyright 2012 Mehfile Zainab. All rights reserved.
//

#import "CustomImageViewer.h"
#import "UIImageExtras.h"

@implementation CustomImageViewer

@synthesize selectedImage;// = _selectedImage;
@synthesize imageView;
@synthesize _images;
@synthesize scrollView;
@synthesize currentPage;

/*
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{		
	
	return imageView;	

}
*/

- (void)viewDidLoad {

	NSLog(@"Start displaying image for current page %d",currentPage);
	
	CGFloat defaultWidth = [[UIScreen mainScreen] bounds].size.width;
	CGFloat defaultHeight = [[UIScreen mainScreen] bounds].size.height - 64.0f;// - 49.0f;
	NSLog(@"Default size : %f , %f", defaultWidth, defaultHeight);
    
	self.selectedImage = [_images objectAtIndex:currentPage];
	
    NSLog(@"Size of selected image : %f , %f", self.selectedImage.size.width, self.selectedImage.size.height);
    
	CGFloat factor = self.selectedImage.size.width / defaultWidth;
	CGFloat frameWidth = defaultWidth;
	CGFloat frameHeight = self.selectedImage.size.height / factor;
	if (frameHeight > defaultHeight) {
		factor = self.selectedImage.size.height / defaultHeight;
		frameHeight = defaultHeight;
		frameWidth = self.selectedImage.size.width / factor;
	}
    
    NSLog(@"Default frame for selected image : %f , %f", frameWidth, frameHeight);
	
	/*
	NSLog(@"Default width : %f",defaultWidth);
	NSLog(@"Default height : %f",defaultHeight);
	NSLog(@"Image initial width : %f",self.selectedImage.size.width);
	NSLog(@"Image initial height : %f",self.selectedImage.size.height);
	NSLog(@"Image width after resizing : %f",frameWidth);
	NSLog(@"Image height after resizing : %f",frameHeight);
	*/
	
	CGFloat xPosition;
	CGFloat yPosition;
	
	if (frameWidth < defaultWidth)
        xPosition = (defaultWidth - frameWidth) / 2;
    else
        xPosition = 0;
	
    if (frameHeight < defaultHeight)
        yPosition = (defaultHeight - frameHeight) / 2;
    else
        yPosition = 0;
	
    NSLog(@"Position for selected image : %f , %f", xPosition, yPosition);
    
	//NSLog(@"Image x position : %f", xPosition);
	//NSLog(@"Image y position : %f", yPosition);
	
	//imageView = [[UIImageView alloc] init];
	//[imageView setImage:[self.selectedImage imageByScalingAndCroppingForSize:CGSizeMake(frameWidth, frameHeight)]];
	//imageView.frame = CGRectMake(xPosition, yPosition, frameWidth, frameHeight);
	
	scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, defaultWidth, defaultHeight)];
	//scrollView.contentSize = CGSizeMake(frameWidth, frameHeight);
	//scrollView.delegate = self;
	//scrollView.maximumZoomScale = 2.0;		
	//scrollView.minimumZoomScale = 0.75;		
	//scrollView.clipsToBounds = YES;
	scrollView.backgroundColor = [UIColor clearColor];
	//[scrollView addSubview:imageView];	
	
	NSUInteger kNumberOfPages = [_images count];
	
	scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(defaultWidth * kNumberOfPages, defaultHeight);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;
	
	self.view = scrollView;
	
	/*
	[self loadScrollViewWithPage:0];
    [self loadScrollViewWithPage:1];
	*/
    
    /*
	[self loadScrollViewWithPage:currentPage - 1];
    [self loadScrollViewWithPage:currentPage];
    [self loadScrollViewWithPage:currentPage + 1];
	*/
    
    for ( int i = 0; i < [_images count]; i++ ) {
		[self loadScrollViewWithPage:i];
	}
    
	CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * currentPage;// + xPosition;
    frame.origin.y = 0 + yPosition;

    NSLog(@"Scroll view frame size : %f , %f", frame.size.width, frame.size.height);
    
    NSLog(@"Scroll view frame origin for selected image : %f , %f", frame.origin.x, frame.origin.y);
    
	[scrollView setContentOffset:CGPointMake(frame.origin.x, frame.origin.y) animated:NO];
	
	[super viewDidLoad];
	
}

- (void)loadScrollViewWithPage:(int)page
{
	
	NSUInteger kNumberOfPages = [_images count];
	
	if (page < 0)
        return;
    if (page >= kNumberOfPages)
        return;
	
	self.selectedImage = [_images objectAtIndex:page];
	
	CGFloat defaultWidth = [[UIScreen mainScreen] bounds].size.width;
	CGFloat defaultHeight = [[UIScreen mainScreen] bounds].size.height - 64.0f;// - 49.0f;
	CGFloat factor = self.selectedImage.size.width / defaultWidth;
	CGFloat frameWidth = defaultWidth;
	CGFloat frameHeight = self.selectedImage.size.height / factor;
	if (frameHeight > defaultHeight) {
		factor = self.selectedImage.size.height / defaultHeight;
		frameHeight = defaultHeight;
		frameWidth = self.selectedImage.size.width / factor;
	}
    
	CGFloat xPosition;
	CGFloat yPosition;
	
	if (frameWidth < defaultWidth)
        xPosition = (defaultWidth - frameWidth) / 2;
    else
        xPosition = 0;
	
    if (frameHeight < defaultHeight)
        yPosition = (defaultHeight - frameHeight) / 2;
    else
        yPosition = 0;
	
    // add the controller's view to the scroll view
	CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page + xPosition;
    frame.origin.y = 0 + yPosition;
	imageView = [[UIImageView alloc] init];
	[imageView setImage:[self.selectedImage imageByScalingAndCroppingForSize:CGSizeMake(frameWidth, frameHeight)]];
	//imageView.frame = frame;
	imageView.frame = CGRectMake(frame.origin.x, frame.origin.y, frameWidth, frameHeight);

    imageView.tag = page;
    
    [scrollView addSubview:imageView];
    [imageView release];
    imageView = nil;
        
}

- (void)unloadScrollViewWithPage:(int)page
{
	/*
	NSUInteger kNumberOfPages = [_images count];
	
	if (page < 0)
        return;
    if (page >= kNumberOfPages)
        return;
	
	self.selectedImage = [_images objectAtIndex:page];
	
	CGFloat defaultWidth = [[UIScreen mainScreen] bounds].size.width;
	CGFloat defaultHeight = [[UIScreen mainScreen] bounds].size.height - 64.0f;
	CGFloat factor = self.selectedImage.size.width / defaultWidth;
	CGFloat frameWidth = defaultWidth;
	CGFloat frameHeight = self.selectedImage.size.height / factor;
	if (frameHeight > defaultHeight) {
		factor = self.selectedImage.size.height / defaultHeight;
		frameHeight = defaultHeight;
		frameWidth = self.selectedImage.size.width / factor;
	}
    
	CGFloat xPosition;
	CGFloat yPosition;
	
	if (frameWidth < defaultWidth)
        xPosition = (defaultWidth - frameWidth) / 2;
    else
        xPosition = 0;
	
    if (frameHeight < defaultHeight)
        yPosition = (defaultHeight - frameHeight) / 2;
    else
        yPosition = 0;
	
    // add the controller's view to the scroll view
	CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page + xPosition;
    frame.origin.y = 0 + yPosition;
	imageView = [[UIImageView alloc] init];
	[imageView setImage:[self.selectedImage imageByScalingAndCroppingForSize:CGSizeMake(frameWidth, frameHeight)]];
	//imageView.frame = frame;
	imageView.frame = CGRectMake(frame.origin.x, frame.origin.y, frameWidth, frameHeight);
    
    [imageView removeFromSuperview];
    */
    for ( int i = 0; i < [_images count]; i++ ) {
		if ( (i < (page-1) || i > (page+1)) && [scrollView viewWithTag:(i+1)] ) {
            NSLog(@"Remove image %d from scrollview",i+1);
			[[scrollView viewWithTag:(i+1)] removeFromSuperview];
		}
	}
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    // Switch the indicator when more than 50% of the previous/next page is visible
    
    
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.navigationItem.title = [NSString stringWithFormat:@"%d / %d",page+1,[_images count]];
    
    
    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    /*
    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
    */
    // A possible optimization would be to unload the views+controllers which are no longer visible
    //[self unloadScrollViewWithPage:page-2];
    //[self unloadScrollViewWithPage:page+2];
    /*
    [self unloadScrollViewWithPage:page];
    */
}

@end
